package net.hymnal.church;

import java.util.ArrayList;

import net.hymnal.church.HymnsManager.HYMNS_TYPE;
import net.hymnal.church.ThemesActivity.Stage;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class GimnsSelectActivity extends Activity {	
	EditText     editSelectNumber;
	EditText     editSearchLine;	
	
	Button   buttonOpenNumber;
	Button   buttonOpenLastNumber;
	Button   buttonOpenThemes;
	Button   buttonSearchLine;
		
	ArrayList<Integer> 			m_arrayNums;	
	private void openHymnActionNumber(View v) {
		HymnsManager.resetSelection();
		String str = editSelectNumber.getText().toString();
		if (str == "") {
			return;
		}

		try {
			int selectedHymn = Integer.decode(str) - 1;
			if((selectedHymn < 0) || (selectedHymn >= HymnsManager.getNumberHymns())) {
				return;
			}
			HymnActivity.startHymnActivity(this, v, selectedHymn);			
		} catch (NumberFormatException nfe) {
			Log.e("", "The string can not be decodet to Int:"
					+ editSelectNumber.getText().toString()
					+ "\n Message:" + nfe.getMessage());
		}				
	}
	
	private void openHymnActionLastNumber(View v) {
		HymnsManager.resetSelection();		

		try {
			int selectedHymn = HymnsManager.getLastSelectedHymn(HymnsManager.getSelectedType());
			HymnActivity.startHymnActivity(this, v, selectedHymn);			
		} catch (NumberFormatException nfe) {
			Log.e("", "The string can not be decodet to Int:"
					+ editSelectNumber.getText().toString()
					+ "\n Message:" + nfe.getMessage());
		}				
	}
	
	private void openHymnActionThemes(View v) {
		ThemesActivity.currentStage = Stage.MAIN_THEME;
		Intent myIntent = new Intent(v.getContext(),
				ThemesActivity.class);
		startActivityForResult(myIntent, 0);		
	}
	
	private void startSearchAction(View v) {
		HymnsManager.resetSelection();		
		String subString = editSearchLine.getText().toString();
		if (subString == "") {					
			return;
		}
		
		if (HymnsManager.SelectHymns(subString)) {
			Intent myIntent = new Intent(v.getContext(),
					SearchResultActivity.class);
			startActivityForResult(myIntent, 0);
		}
	}
	
	TextView.OnEditorActionListener keyboardListener = new TextView.OnEditorActionListener() {
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			if (v == (View)editSearchLine && actionId == EditorInfo.IME_ACTION_SEARCH) {
				startSearchAction(v);
				return true;
			}
			if (v == (View)editSelectNumber 
					&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER
					&& event.getAction() == KeyEvent.ACTION_DOWN) {
				openHymnActionNumber(v);
				return true;
			}
			
			return false;
		}
	};
	
	@SuppressLint("ShowToast")
	OnClickListener buttonClickListener = new View.OnClickListener(){
		public void onClick(View view) {
			if (view == buttonSearchLine) {
				startSearchAction(view);
				return;
			}
			
			if (view == buttonOpenNumber) {
				openHymnActionNumber(view);
				return;
			}
			
			if (view == buttonOpenLastNumber) {
				openHymnActionLastNumber(view);
				return;
			}
			
			
			if (view == buttonOpenThemes) {
				openHymnActionThemes(view);
				return;
			}
		}		
	};	
	
	void initControls() {
       
		buttonOpenNumber = (Button) findViewById(R.id.button_open_hymn_number);
		buttonOpenNumber.setOnClickListener(buttonClickListener);
		
		buttonOpenLastNumber = (Button)findViewById(R.id.button_open_last_hymn_number);
		buttonOpenLastNumber.setOnClickListener(buttonClickListener);
		
		buttonOpenThemes = (Button) findViewById(R.id.button_open_hymn_themes);
		buttonOpenThemes.setOnClickListener(buttonClickListener);		
		HYMNS_TYPE sType = HymnsManager.getSelectedType();
		if(sType == HYMNS_TYPE.TYPE_YOUNG) {
			buttonOpenThemes.setVisibility(View.GONE);
		}
		
		editSelectNumber = (EditText) findViewById(R.id.edit_open_number);
		editSelectNumber.setOnEditorActionListener(keyboardListener);

		editSearchLine = (EditText) findViewById(R.id.edit_search_line);
		editSearchLine.setOnEditorActionListener(keyboardListener); 
	}
	
	@Override
	public void onResume() {
		super.onResume();
		editSelectNumber.setText("");
		int lastNumber = HymnsManager.getLastSelectedHymn(HymnsManager.getSelectedType());
		String str = Integer.toString(lastNumber + 1);
		buttonOpenLastNumber.setText(str);

		openSoftKeyboard();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	setTheme(GimnsMain.mSettings.getCurrentTheme());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_select_search);        
        initControls();
    } 
    
    
    private void openSoftKeyboard() {
    	(new Handler()).postDelayed(new Runnable() {

            public void run() {
                EditText yourEditText= (EditText) findViewById(R.id.edit_open_number);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
            }
        }, 200);
    	
    }
}